/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.model.bo.excel;

import cn.ac.nercis.pes.model.bo.excel.asset.*;
import cn.ac.nercis.pes.common.constant.excel.AssetExcelSheet;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Excel文档中的资产对象信息
 * @author zpy
 * @version 1.0.0
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class AssetExcelBO {
    private List<PhyEnvBO> phyEnvs;
    private List<PhySecurityBO> phySecurity;
    private List<PwdProductBO> pwdProducts;
    private List<ServerStorageBO> serverStorages;
    private List<NetworkSecurityBO> networkSecurity;
    private List<DatabaseBO> databases;
    private List<KeyBusinessBO> keyBusiness;
    private List<SubBusinessBO> subBusiness;
    private List<KeyDataBO> keyData;
    private List<SecurityDocumentBO> securityDocuments;
    private List<PersonnelBO> personnel;

    @Builder.Default
    private Boolean globalCover = true;

    public boolean isEmpty(){
        return CollectionUtils.isEmpty(phyEnvs)
                && CollectionUtils.isEmpty(phySecurity)
                && CollectionUtils.isEmpty(pwdProducts)
                && CollectionUtils.isEmpty(serverStorages)
                && CollectionUtils.isEmpty(networkSecurity)
                && CollectionUtils.isEmpty(databases)
                && CollectionUtils.isEmpty(keyBusiness)
                && CollectionUtils.isEmpty(subBusiness)
                && CollectionUtils.isEmpty(keyData)
                && CollectionUtils.isEmpty(securityDocuments)
                && CollectionUtils.isEmpty(personnel);
    }

    public List<String> findAllSheet(){
        List<String> assetClass = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(phyEnvs)){
            assetClass.add(AssetExcelSheet.PHY_ENV.getName());
        }
        if(CollectionUtils.isNotEmpty(phySecurity)){
            assetClass.add(AssetExcelSheet.PHY_SECURITY.getName());
        }
        if(CollectionUtils.isNotEmpty(pwdProducts)){
            assetClass.add(AssetExcelSheet.PWD_PRODUCT.getName());
        }
        if(CollectionUtils.isNotEmpty(serverStorages)){
            assetClass.add(AssetExcelSheet.SERVER_STORAGE.getName());
        }
        if(CollectionUtils.isNotEmpty(networkSecurity)){
            assetClass.add(AssetExcelSheet.NETWORK_SECURITY.getName());
        }
        if(CollectionUtils.isNotEmpty(databases)){
            assetClass.add(AssetExcelSheet.DATABASE.getName());
        }
        if(CollectionUtils.isNotEmpty(keyBusiness)){
            assetClass.add(AssetExcelSheet.KEY_BUSINESS.getName());
        }
        if(CollectionUtils.isNotEmpty(subBusiness)){
            assetClass.add(AssetExcelSheet.SUB_BUSINESS.getName());
        }
        if(CollectionUtils.isNotEmpty(keyData)){
            assetClass.add(AssetExcelSheet.KEY_DATA.getName());
        }
        if(CollectionUtils.isNotEmpty(securityDocuments)){
            assetClass.add(AssetExcelSheet.SECURITY_DOC.getName());
        }
        if(CollectionUtils.isNotEmpty(personnel)){
            assetClass.add(AssetExcelSheet.PERSONNEL.getName());
        }
        return assetClass;
    }

    public boolean hasValidFailed(){
        return phyEnvs.stream().anyMatch(sd-> !sd.getResult())
                || phySecurity.stream().anyMatch(sd-> !sd.getResult())
                || pwdProducts.stream().anyMatch(sd-> !sd.getResult())
                || serverStorages.stream().anyMatch(sd-> !sd.getResult())
                || networkSecurity.stream().anyMatch(sd-> !sd.getResult())
                || databases.stream().anyMatch(sd-> !sd.getResult())
                || keyBusiness.stream().anyMatch(sd-> !sd.getResult())
                || subBusiness.stream().anyMatch(sd-> !sd.getResult())
                || keyData.stream().anyMatch(sd-> !sd.getResult())
                || securityDocuments.stream().anyMatch(sd-> !sd.getResult())
                || personnel.stream().anyMatch(sd-> !sd.getResult());
    }

    public boolean hasValidFailed(String sheet){
        AssetExcelSheet assetClass = AssetExcelSheet.fromName(sheet);
        boolean result = switch (assetClass) {
            case PHY_ENV -> phyEnvs.stream().anyMatch(sd -> !sd.getResult());
            case PHY_SECURITY -> phySecurity.stream().anyMatch(sd -> !sd.getResult());
            case PWD_PRODUCT -> pwdProducts.stream().anyMatch(sd -> !sd.getResult());
            case SERVER_STORAGE -> serverStorages.stream().anyMatch(sd -> !sd.getResult());
            case NETWORK_SECURITY -> networkSecurity.stream().anyMatch(sd -> !sd.getResult());
            case DATABASE -> databases.stream().anyMatch(sd -> !sd.getResult());
            case KEY_BUSINESS -> keyBusiness.stream().anyMatch(sd -> !sd.getResult());
            case SUB_BUSINESS -> subBusiness.stream().anyMatch(sd -> !sd.getResult());
            case KEY_DATA -> keyData.stream().anyMatch(sd -> !sd.getResult());
            case SECURITY_DOC -> securityDocuments.stream().anyMatch(sd -> !sd.getResult());
            case PERSONNEL -> personnel.stream().anyMatch(item -> !item.getResult());
        };
        return result;
    }
}